﻿using System.Threading.Tasks;
using Controller;
using NN = NNCore;

namespace GACore.ReversiGA
{
	public class NNvsKITournament : Tournament
	{
		public NNvsKITournament(NN.Controller[] nnControllers)
			: base(nnControllers)
		{
		}

		public override void PlayTournament(Vector[] vectors)
		{
			base.PlayTournament(vectors);

			Parallel.For(0, vectors.Length,
				i =>
				{
					Vector<double> v = (Vector<double>)vectors[i];
					vectors[i].Fitness.Reset();
					nnControllers[i].SetNetAsVector(v.Elements);

					for (int k = 0; k < 5; k++)
					{
						PlayGames(i);
					}

					vectors[i].Fitness.ComputeAvg();
				});
		}

		private void PlayGames(int idNN)
		{
			PlayGame(PlayerTypes.NN, PlayerTypes.KI, idNN, -1);
			PlayGame(PlayerTypes.KI, PlayerTypes.NN, -1, idNN);
		}
	}
}
